<?php

namespace app\admin\command;

use app\admin\model\cms\GrapevineModel;
use app\admin\model\cms\Live;
use app\admin\model\CutWordIndex;
use app\admin\model\Investment;
use app\api\service\FetchArticleService;
use app\api\service\JieBaFenCi;
use app\api\service\TengxunSignatureUtil;
use app\common\model\Attachment;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Exception;

/**
 * 定时任务-定时插入关键词索引表
 * Class UpdateCutWordIndex
 * @package app\admin\command
 */
class UpdateCutWordIndex extends Command
{
    protected function configure()
    {
        $this->setName('updateCutWordIndex')->setDescription('定时插入关键词索引表');
    }

    protected function execute(Input $input, Output $output)
    {
        $jieBaFenCi = new JieBaFenCi();
        // 执行具体的任务逻辑
        $not_id = CutWordIndex::where('table_name', 'investment')->distinct(true)->column('a_id');
        $data = Investment::
        whereNotIn('id', $not_id)
//        where('id',14)
            ->field('id,investment_category_id,macro_content,policy_content,fund_content,emotion_content,expect_content,news_content,file_list')
            ->select();

        $begin = time();
        $count = 0;
        $create_data = [];
        foreach ($data as $v) {
            //dump('---------------' . $v['id']);
            $str_macro_content = strip_tags($v['macro_content']);
            if ($str_macro_content) {
                $str_arr = $jieBaFenCi->cutStatement($str_macro_content);
                //分好词的一维数组
                if ($str_arr) {
                    $create_data[] = $this->createData($v['id'], 'macro_content', $str_arr, 'investment');
                }
            }
            //dump(time() - $begin);

            $str_policy_content = strip_tags($v['policy_content']);
            if ($str_policy_content) {
                $str_arr = $jieBaFenCi->cutStatement($str_policy_content);
                //分好词的一维数组
                if ($str_arr) {
                    $create_data[] = $this->createData($v['id'], 'policy_content', $str_arr, 'investment');
                }
            }
            //dump(time() - $begin);

            $str_fund_content = strip_tags($v['fund_content']);
            if ($str_fund_content) {
                $str_arr = $jieBaFenCi->cutStatement($str_fund_content);
                //分好词的一维数组
                if ($str_arr) {
                    $create_data[] = $this->createData($v['id'], 'fund_content', $str_arr, 'investment');
                }

            }
            //dump(time() - $begin);

            $str_emotion_content = strip_tags($v['emotion_content']);
            if ($str_emotion_content) {
                $str_arr = $jieBaFenCi->cutStatement($str_emotion_content);
                //分好词的一维数组
                if ($str_arr) {
                    $create_data[] = $this->createData($v['id'], 'emotion_content', $str_arr, 'investment');
                }

            }
            //dump(time() - $begin);

            $str_expect_content = strip_tags($v['expect_content']);
            if ($str_expect_content) {
                $str_arr = $jieBaFenCi->cutStatement($str_expect_content);
                //分好词的一维数组
                if ($str_arr) {
                    $create_data[] = $this->createData($v['id'], 'expect_content', $str_arr, 'investment');
                }

            }
            //dump(time() - $begin);

            $str_news_content = strip_tags($v['news_content']);
            if ($str_news_content) {
                $str_arr = $jieBaFenCi->cutStatement($str_news_content);
                //分好词的一维数组
                if ($str_arr) {
                    $create_data[] = $this->createData($v['id'], 'news_content', $str_arr, 'investment');
                }
            }
            //dump(time() - $begin);

            //文件列表
            if ($v['file_list']){
                $str_file_list = json_decode($v['file_list'],JSON_UNESCAPED_UNICODE);
                $str_file_list = array_column($str_file_list, "name");
                $str_file_list = implode(',',$str_file_list);
                $str_arr = $jieBaFenCi->cutStatement($str_file_list);
                //分好词的一维数组
                if ($str_arr) {
                    $create_data[] = $this->createData($v['id'], 'file_list', $str_arr, 'investment');
                }
            }
            //dump(time() - $begin);
        }

        //入库分词表
        $r = CutWordIndex::insertAll($create_data);
        //$count = $count + $r;
        //dd('入库结果=' . $count);
    }

    //制造存储数据
    private function createData($aid, $field_name, $str_arr, $table_name = 'investment')
    {
        $cur_arr = array_unique($str_arr);//去重
        //存入投研日志文章检索索引表
        $cur_arr = join(' ', $cur_arr);
        $create_data = [
            'a_id' => $aid,//文章
            'field_name' => $field_name,
            'key_word' => $cur_arr,
            'table_name' => $table_name
        ];
        return $create_data;
    }


}